package com.mem.barrier;

public class VolatileDemo {

    public static volatile int found = 0;
//    public static int found = 0;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                System.out.println("辅助线程送笔来...");
                long i = 1;
                if (0 == found) {
                    i++;
//                    if (i % 100 == 0)
//                        System.out.println("还没送过来" + i);
                }
                System.out.println("笔来了，开始写字..." + i);
            }
        }, "main线程").start();

        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("辅助线程找到笔了，送过去...");

                change();
            }
        }, "辅助线程").start();
    }

    public static void change() {
        found = 1;
    }
}